package com.ulic.customerManApplication.achievementmodule.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.achievementmodule.bo.Achievement;
import com.ulic.customerManApplication.achievementmodule.dao.AchievementDao;
import com.ulic.customerManApplication.achievementmodule.vo.AchievementVO;
import com.ulic.customerManApplication.base.BaseDao;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.usermodule.bo.User;

@Component("achievementDao")
public class AchievementDaoImpl extends BaseDao implements AchievementDao {

	private Session session;
	@Override
	public void deleteAchievement(Achievement achievement) throws Exception {

		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.delete(achievement);
		session.flush();
	}

	@Override
	public Achievement getAchievement(int id) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Achievement.class);
		List list = cri.add(Restrictions.eq("id", id)).list();
		Achievement achievement = null;
		if(list.size()>0){
			achievement = (Achievement)list.get(0);
		}
		return achievement;
	}

	@Override
	public PagerModel getAchievements(int curPage) {
		String sql = " from Achievement achievement order by createDate desc";
		return searchPaginated(sql, curPage, CONSTANT.PageSize);
	}

	@Override
	public List<Achievement> getAchievements(User user) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Achievement.class);
		List list = cri.createAlias("user", "u").add(Restrictions.eq("u.um_user_id", user.getUm_user_id())).list();
		return list;
	}

	@Override
	public PagerModel getAchievements(AchievementVO achievementVO, int curPage) {

		StringBuilder sb = new StringBuilder();
		sb.append(" from Achievement achievement where 1=1");
		Achievement achievement = achievementVO.getAchievement();
		if(achievement != null){
			if(achievement.getAchieveName() != null && achievement.getAchieveName().length() !=0){
				sb.append(" and achievement.achieveName  like '%"+achievement.getAchieveName().trim()+"%'");
			}
			if(achievement.getAchieveNumber() != null && achievement.getAchieveNumber().length() !=0){
				sb.append(" and achievement.achieveNumber  like '%"+achievement.getAchieveNumber().trim()+"%'");
			}
			if(achievement.getSchool()!= null && achievement.getSchool().length() != 0){
				sb.append(" and achievement.school like '%"+achievement.getSchool().trim()+"%'");
			}
			if(achievement.getSubCompany()!= null&&!"No".equals(achievement.getSubCompany())){
				sb.append(" and achievement.subCompany = '"+achievement.getSubCompany()+"'");
			}
			if(achievement.getSubCity()!= null&&!"No".equals(achievement.getSubCity())){
				sb.append(" and achievement.subCity = '"+achievement.getSubCity()+"'");
			}
			if(achievementVO.getStartcreate()!=null &&achievementVO.getStartcreate().length()!=0){
				sb.append(" and achievement.createDate>= to_date('"+achievementVO.getStartcreate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(achievementVO.getEndcreate()!=null &&achievementVO.getEndcreate().length()!=0){
				sb.append(" and achievement.createDate <= to_date('"+achievementVO.getEndcreate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
		}
		sb.append(" order by createDate desc");
		return searchPaginated(sb.toString(), curPage, CONSTANT.PageSize);
	}

	@Override
	public void insertAchievement(Achievement achievement) throws Exception {

		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		try{
			session.save(achievement);
		}catch (Exception e) {
			e.printStackTrace();
		}
		session.flush();
	}

	@Override
	public void insertAchievements(List<Achievement> achievements)
			throws Exception {

		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		for(Achievement achievement:achievements){
			session.save(achievement);
		}
		session.flush();
	}

	@Override
	public void updateAchievement(Achievement achievement) throws Exception {

		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.saveOrUpdate(achievement);
		session.flush();
	}

	@Override
	public PagerModel getAchievements(AchievementVO achievementVO, int curPage,
			int totalNum) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from Achievement achievement where 1=1");
		Achievement achievement = achievementVO.getAchievement();
		if(achievement != null){
			if(achievement.getAchieveName() != null && achievement.getAchieveName().length() !=0){
				sb.append(" and achievement.achieveName  like '%"+achievement.getAchieveName().trim()+"%'");
			}
			if(achievement.getAchieveNumber() != null && achievement.getAchieveNumber().length() !=0){
				sb.append(" and achievement.achieveNumber  like '%"+achievement.getAchieveNumber().trim()+"%'");
			}
			if(achievement.getSchool()!= null && achievement.getSchool().length() != 0){
				sb.append(" and achievement.school like '%"+achievement.getSchool().trim()+"%'");
			}
			if(achievement.getSubCompany()!= null&&!"No".equals(achievement.getSubCompany())){
				sb.append(" and achievement.subCompany = '"+achievement.getSubCompany()+"'");
			}
			if(achievement.getSubCity()!= null&&!"No".equals(achievement.getSubCity())){
				sb.append(" and achievement.subCity = '"+achievement.getSubCity()+"'");
			}
			if(achievementVO.getStartcreate()!=null &&achievementVO.getStartcreate().length()!=0){
				sb.append(" and achievement.createDate>= to_date('"+achievementVO.getStartcreate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(achievementVO.getEndcreate()!=null &&achievementVO.getEndcreate().length()!=0){
				sb.append(" and achievement.createDate <= to_date('"+achievementVO.getEndcreate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
		}
		sb.append(" order by createDate desc");
		return searchPaginated(sb.toString(), curPage, totalNum);
	}

}
